package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.dto.PayOrderDataDto;
import cn.itsource.ymcc.service.IPayOrderService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Date;

@Component
@RocketMQMessageListener(topic = "payorder-topic",selectorExpression = "payorder-tag",consumerGroup = "PayMesGroup",messageModel = MessageModel.CLUSTERING)
public class PayOrderConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IPayOrderService payOrderService;
    @Override
    public void onMessage(MessageExt messageExt) {
        if (messageExt.getBody() == null || messageExt.getBody().length == 0)return;
        String s = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        PayOrderDataDto dto = JSON.parseObject(s, PayOrderDataDto.class);
        // 判断订单是否存在
        PayOrder payOrder = payOrderService.selectByOrderNo(dto.getOrderNo());
        if (payOrder == null) payOrder = new PayOrder();
        BeanUtils.copyProperties(dto,payOrder);
        payOrder.setCreateTime(new Date());
        payOrder.setPayStatus(0);
        payOrderService.insert(payOrder);

    }
}
